package com.jianyun.jyzs.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.LoginActivity;
import com.jianyun.jyzs.adapter.SimpleAdapter;
import com.jianyun.jyzs.bean.EnterInfo;
import com.jianyun.jyzs.dao.LoginDao;

import java.util.List;

/**
 * Created by liml on 17/6/21.
 */

public class MoreWriteEditText extends RelativeLayout implements View.OnFocusChangeListener, TextWatcher {

    private TextView cwtTitle;
    private EditText cwtContent;
    private CheckBox cwtMore;
    private View cwtUnderline;
    private View cwtUnderlineFocus;
    private boolean underLineVisible;
    private Context context;
    private TextWatcher textWatcher;
    private PopupWindow popupWindow = null;

    public MoreWriteEditText(Context context) {
        super(context);
        this.context = context;
        initView(null);
    }

    public MoreWriteEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView(attrs);
    }
    private void initView(AttributeSet attrs) {
        inflate(getContext(), R.layout.more_view_clear_write_edit_text, this);
        cwtTitle = (TextView) findViewById(R.id.cwt_title);
        cwtContent = (EditText) findViewById(R.id.cwt_content);
        cwtMore = (CheckBox) findViewById(R.id.cwt_clear);
        cwtUnderline = findViewById(R.id.cwt_underline);
        cwtUnderlineFocus = findViewById(R.id.cwt_underline_focus);
        if (attrs != null) {
            TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.rce_cwt_edit_text);
            CharSequence title = attributes.getText(R.styleable.rce_cwt_edit_text_cwt_edit_text_title);
            if (!TextUtils.isEmpty(title)) {
                cwtTitle.setText(title);
            } else {
                cwtTitle.setVisibility(View.GONE);
            }
            int contentColor = attributes.getColor(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_color, getResources().getColor(R.color.color_normal_text));
            cwtContent.setTextColor(contentColor);
            float textSize = attributes.getDimension(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_textSizes, -1);
            if (textSize != -1) {
                cwtContent.setTextSize(textSize);
            }
            int maxLines = attributes.getInteger(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_maxLines, -1);
            if (maxLines != -1) {
                cwtContent.setMaxLines(maxLines);
            }
            CharSequence text = attributes.getText(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_text);
            cwtContent.setText(text);
            int maxLength = attributes.getInteger(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_maxLength, -1);
            if (maxLength != -1) {
                cwtContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
            }
            CharSequence hint = attributes.getText(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_hint);
            if (!TextUtils.isEmpty(hint)) {
                cwtContent.setHint(hint);
            }
            boolean singleLine = attributes.getBoolean(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_singleLine, false);
            if (singleLine) {
                cwtContent.setSingleLine();
            }
            String inputType = attributes.getString(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_inputType);
            if (!TextUtils.isEmpty(inputType)) {
                if (inputType.equals("textPassword")) {
                    cwtContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else if (inputType.equals("number")) {
                    cwtContent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
                }
            }
            underLineVisible = attributes.getBoolean(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_underLineVisible, true);
            if (!underLineVisible) {
                cwtUnderline.setVisibility(View.GONE);
            }
            float marginLeft = attributes.getDimension(R.styleable.rce_cwt_edit_text_cwt_edit_text_content_margin_left, -1);
            if (marginLeft != -1) {
                LayoutParams params = (LayoutParams) cwtContent.getLayoutParams();
                params.setMargins((int) marginLeft, 0, (int) getResources().getDimension(R.dimen.rce_dimen_size_14), 0);
                cwtContent.setLayoutParams(params);
            }
            attributes.recycle();
        }
        cwtContent.setOnFocusChangeListener(this);
        cwtContent.addTextChangedListener(this);
        cwtMore.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    cwtMore.setBackgroundResource(R.drawable.ic_regain);
                    showPopu();
                }else {
                    cwtMore.setBackgroundResource(R.drawable.ic_expand_small);
                    popupWindow.dismiss();
                }
            }
        });

    }
    private void showPopu() {
        if (popupWindow==null){
            final View view = LayoutInflater.from(context).inflate(R.layout.list_view, null);
            popupWindow = new PopupWindow(view, this.getWidth(), LinearLayout.LayoutParams.WRAP_CONTENT);
            ListView lv = (ListView) view.findViewById(R.id.listview);
            LoginDao loginDao = new LoginDao(context);
            final List<EnterInfo> listData = loginDao.getListData();
            //Log.i("test","查询："+listData.get(0).toString());
            SimpleAdapter adapter = new SimpleAdapter(loginDao,context,listData);
            lv.setAdapter(adapter);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    EnterInfo info = listData.get(position);
                    ((LoginActivity)context).loginAccount.setText(info.getAccount());
                    ((LoginActivity)context).loginPwd.setText(info.getPassword());
                    ((LoginActivity)context).loginEnterprise.setText(info.getEnterinfo());

                }
            });
        }
        popupWindow.showAsDropDown(this);
    }
    public String getText() {
        return cwtContent.getText().toString();
    }
    public void setText(String text){
        cwtContent.setText(text);
        dismissPopu();

    }

    private void dismissPopu() {
        cwtMore.setChecked(false);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
            if (popupWindow!=null)popupWindow.dismiss();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        if (textWatcher != null) {
            textWatcher.beforeTextChanged(s, start, count, after);
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (textWatcher != null) {
            textWatcher.onTextChanged(s, start, before, count);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (textWatcher != null) {
            textWatcher.afterTextChanged(s);
        }
    }
    public void addTextChangedListener(TextWatcher textWatcher) {
        this.textWatcher = textWatcher;
    }
}
